package trabalho3.model;

import java.awt.image.BufferedImage;
import java.awt.image.PixelGrabber;

import model.org.jtrace.primitives.ColorRGB;

public class ImageTexture {
	private int[] imagePixels;
	private int imageWidth, imageHeight;
	//private BufferedImage image;
	
	public ImageTexture (BufferedImage image, int width, int height) throws InterruptedException {
		//this.image = image;
		
		PixelGrabber grabber = new PixelGrabber (image, 0, 0, width, height, true);
		
		if (!grabber.grabPixels ()) {
			throw new IllegalArgumentException ("Invalid image; pixel grab failed.");
		}
		
		imagePixels = (int[]) grabber.getPixels();
		imageWidth = grabber.getWidth();
		imageHeight = grabber.getHeight();
	}
	
	public ColorRGB getTexel(double x, double y) {
		return new ColorRGB (imagePixels[(int) (x * imageWidth % imageWidth) + imageWidth * (int) (y * imageHeight % imageHeight)]);
		
//		int sourceX = (int) i;
//		int sourceY = (int) j;
//		return new ColorRGB (imagePixels[sourceX+sourceY*imageWidth]);
	}
	
	public int getImageWidth() {
		return this.imageWidth;
	}
	
	public int getImageHeight() {
		return this.imageHeight;
	}
}
